package com.joizhang.seckill.mapper;

import com.joizhang.seckill.model.Order;
import com.joizhang.seckill.model.OrderInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

@Repository
@Mapper
public interface OrderMapper {

    @Select("select count(*) from sk_order where user_id=#{userId} and goods_id=#{goodsId}")
    Integer countOrderByUserIdGoodsId(@Param("userId") Long userId, @Param("goodsId") Long goodsId);

    @Select("select * from sk_order where user_id=#{userId} and goods_id=#{goodsId}")
    Order getOrderByUserIdGoodsId(@Param("userId") Long userId, @Param("goodsId") Long goodsId);

    @Insert("insert into sk_order (order_info_id, user_id, goods_id) values (#{orderInfoId}, #{userId}, #{goodsId})")
    void insertOrder(Order order);

    /**
     * 通过@SelectKey使insert成功后返回主键id，也就是订单id
     */
    @Insert("insert into sk_order_info(user_id, goods_id, goods_name, goods_price, goods_count, goods_pay, order_channel, status, delivery_addr_id, create_date) " +
            "values (#{userId}, #{goodsId}, #{goodsName}, #{goodsPrice}, #{goodsCount}, #{goodsPay}, #{orderChannel}, #{status}, #{deliveryAddrId}, #{createDate})")
    @SelectKey(keyColumn = "id", keyProperty = "id", resultType = Long.class, before = false, statement = "select last_insert_id()")
    void insertOrderInfo(OrderInfo orderInfo);

    @Select("select * from sk_order_info where id=#{orderId}")
    OrderInfo getOrderInfoById(@Param("orderId") Long orderId);

}
